/*
 * 左边工具栏菜单按钮点击
 * name : 菜单名称
 */
var onLeftPanelMenuClick = function (name) {
  var btntoopt = document.getElementById(name);
  btntoopt.className = "menu-select";
  if (name === "devinfo") {
    btntoopt = document.getElementById("sipconfig");
    btntoopt.className = "menu";
    btntoopt = document.getElementById("rtpmulticastconfig");
    btntoopt.className = "menu";
    btntoopt = document.getElementById("audioconfig");
    btntoopt.className = "menu";
    btntoopt = document.getElementById("netconfig");
    btntoopt.className = "menu";
    parent.mainFrame.location.href = "devinfo.html"
  }
  else if (name === "sipconfig") {
    btntoopt = document.getElementById("devinfo");
    btntoopt.className = "menu";
    btntoopt = document.getElementById("rtpmulticastconfig");
    btntoopt.className = "menu";
    btntoopt = document.getElementById("audioconfig");
    btntoopt.className = "menu";
    btntoopt = document.getElementById("netconfig");
    btntoopt.className = "menu";
    parent.mainFrame.location.href = "sipconfig.html"
  }
  else if (name === "rtpmulticastconfig") {
    btntoopt = document.getElementById("devinfo");
    btntoopt.className = "menu";
    btntoopt = document.getElementById("sipconfig");
    btntoopt.className = "menu";
    btntoopt = document.getElementById("audioconfig");
    btntoopt.className = "menu";
    btntoopt = document.getElementById("netconfig");
    btntoopt.className = "menu";
    parent.mainFrame.location.href = "rtpmulticastconfig.html"
  }
  else if (name === "audioconfig") {
    btntoopt = document.getElementById("devinfo");
    btntoopt.className = "menu";
    btntoopt = document.getElementById("sipconfig");
    btntoopt.className = "menu";
    btntoopt = document.getElementById("rtpmulticastconfig");
    btntoopt.className = "menu";
    btntoopt = document.getElementById("netconfig");
    btntoopt.className = "menu";
    parent.mainFrame.location.href = "audioconfig.html"
  }
  else if (name === "netconfig") {
    btntoopt = document.getElementById("devinfo");
    btntoopt.className = "menu";
    btntoopt = document.getElementById("sipconfig");
    btntoopt.className = "menu";
    btntoopt = document.getElementById("rtpmulticastconfig");
    btntoopt.className = "menu";
    btntoopt = document.getElementById("audioconfig");
    btntoopt.className = "menu";
    parent.mainFrame.location.href = "netconfig.html"
  }
}

/*
 * 创建XHR
 */
function createXHR() {
  try {
    // Firefox, Opera 8.0+, Safari
    var xhr = new XMLHttpRequest();
  }
  catch (e) {
    // Internet Explorer
    try {
      var xhr = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
        var xhr = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
        alert("您的浏览器不支持AJAX！");
        return false;
      }
    }
  }
  return xhr;
}

/*
 * 解析设备信息返回值
 */
function devInfoParse(retValue) {
  var str0 = retValue.split('/');
  str0.forEach(function (eachData) {
    //以=切割字符串	
    var str1 = eachData.split("=");
    //console.log(str1[0]);
    switch (str1[0]) {
      case "SipServerIP":
        document.getElementById("SipServerIP").value = str1[1];
        break;
      case "SipAccount":
        document.getElementById("SipAccount").value = str1[1];
        break;
      case "SipState":
        document.getElementById("SipState").value = str1[1];
        break;
      case "EquType":
        document.getElementById("EquType").value = str1[1];
        break;
      case "HardwareVersion":
        document.getElementById("HardwareVersion").value = str1[1];
        break;
      case "SoftwareVersion":
        document.getElementById("SoftwareVersion").value = str1[1];
        break;
      case "AudioOutVolume":
        document.getElementById("AudioOutVolume").value = str1[1];
        break;
      case "MicVolume":
        document.getElementById("MicVolume").value = str1[1];
        break;
      case "IPAddr":
        document.getElementById("IPAddr").value = str1[1];
        break;
      case "NetMask":
        document.getElementById("NetMask").value = str1[1];
        break;
      case "GatewayIP":
        document.getElementById("GatewayIP").value = str1[1];
        break;
      case "MacAddress":
        document.getElementById("MacAddress").value = str1[1];
        break;
      case "readend":
        xhr.abort();
        break;
    }
  })
}

/*
 * 解析Sip配置信息返回值
 */
function sipConfigInfoParse(retValue) {
  var str0 = retValue.split('/');
  str0.forEach(function (eachData) {
    //以=切割字符串	
    var str1 = eachData.split("=");
    //console.log(str1[0]);
    switch (str1[0]) {
      case "SipAccount":
        document.getElementById("SipAccount").value = str1[1];
        break;
      case "SipPasswd":
        document.getElementById("SipPasswd").value = str1[1];
        break;
      case "SipDesc":
        document.getElementById("SipDesc").value = str1[1];
        break;
      case "SipServerIP":
        document.getElementById("SipServerIP").value = str1[1];
        break;
      case "SipServerPort":
        document.getElementById("SipServerPort").value = str1[1];
        break;
      case "SipEnable":
        if (str1[1] == "1") {
          document.getElementById("SipEnable").setAttribute("checked", "checked");
        }
        else {
          document.getElementById("SipEnable").removeAttribute("checked");
        }
        break;
      case "readend":
        xhr.abort();
        break;
    }
  })
}

/*
 * 解析音频配置信息返回值
 */
function audioConfigInfoParse(retValue) {
  var str0 = retValue.split('/');
  str0.forEach(function (eachData) {
    //以=切割字符串	
    var str1 = eachData.split("=");
    switch (str1[0]) {
      case "AudioInEnable":
        document.getElementById("AudioInEnable").value = str1[1];
        break;
      case "AiVolume":
        document.getElementById("AiVolume").value = str1[1];
        break;
      case "AoVolume":
        document.getElementById("AoVolume").value = str1[1];
        break;
      case "HpfStopFreq":
        document.getElementById("HpfStopFreq").value = str1[1];
        break;
      case "readend":
        xhr.abort();
        break;
    }
  })
}

/*
 * 解析Rtp多播配置信息返回值
 */
function rtpMultiCastConfigInfoParse(retValue) {
  var str0 = retValue.split('/');
  str0.forEach(function (eachData) {
    //以=切割字符串	
    var str1 = eachData.split("=");
    if (str1[0] == "readend") {
      xhr.abort();
    }
    else {
      document.getElementById(str1[0]).value = str1[1];
    }
  })
}

/*
 * 解析网络配置信息返回值
 */
function netConfigInfoParse(retValue) {
  var str0 = retValue.split('/');
  str0.forEach(function (eachData) {
    //以=切割字符串	
    var str1 = eachData.split("=");
    switch (str1[0]) {
      case "IPAddr":
        document.getElementById("IPAddr").value = str1[1];
        break;
      case "NetMask":
        document.getElementById("NetMask").value = str1[1];
        break;
      case "GatewayIP":
        document.getElementById("GatewayIP").value = str1[1];
        break;
      case "SipEnable":
        if (str1[1] == "1") {
          document.getElementById("SipEnable").setAttribute("checked", "checked");
        }
        else {
          document.getElementById("SipEnable").removeAttribute("checked");
        }
        break;
      case "readend":
        xhr.abort();
        break;
    }
  })
}

/*
 * 获取设备信息
 */
function getDevInfo() {
  xhr = createXHR();
  if (xhr) {
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4) {
        if (xhr.status == 200) {
          var retValue = xhr.responseText;
          if (retValue != null && retValue.length > 0) {
            console.log(retValue);
            devInfoParse(retValue);
          }
          else {
            console.log("结果为空");
          }
        }
      }
    };
    xhr.open("get", "/cgi-bin/htmlrequest.cgi?GET_DEV_INFO");
    xhr.send(null);
  }
}

/*
 * 恢复默认设置
 */
function resetDefaultConfig() {
  xhr = createXHR();
  if (xhr) {
    xhr.open("get", "/cgi-bin/htmlrequest.cgi?RESET_DEFAULT_CONFIG");
    xhr.send(null);
  }
}

/*
 * 获取Sip配置信息
 */
function getSipConfigInfo() {
  xhr = createXHR();
  if (xhr) {
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4) {
        if (xhr.status == 200) {
          var retValue = xhr.responseText;
          if (retValue != null && retValue.length > 0) {
            console.log(retValue);
            sipConfigInfoParse(retValue);
          }
          else {
            console.log("结果为空");
          }
        }
      }
    };
    xhr.open("get", "/cgi-bin/htmlrequest.cgi?GET_SIPCONFIG_INFO");
    xhr.send(null);
  }
}

/*
 * 获取音频配置信息
 */
function getAudioConfigInfo() {
  xhr = createXHR();
  if (xhr) {
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4) {
        if (xhr.status == 200) {
          var retValue = xhr.responseText;
          if (retValue != null && retValue.length > 0) {
            console.log(retValue);
            audioConfigInfoParse(retValue);
          }
          else {
            console.log("结果为空");
          }
        }
      }
    };
    xhr.open("get", "/cgi-bin/htmlrequest.cgi?GET_AUDIOCONFIG_INFO");
    xhr.send(null);
  }
}

/*
 * 播放测试音频
 */
function ctlTestAudio() {
  xhr = createXHR();
  if (xhr) {
    xhr.open("get", "/cgi-bin/htmlrequest.cgi?CTL_AUDIOCONFIG_PLAY_TESTAUDIO");
    xhr.send(null);
  }
}

/*
 * 获取rtp多播配置
 */
function getRtpMultiCastConfig() {
  xhr = createXHR();
  if (xhr) {
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4) {
        if (xhr.status == 200) {
          var retValue = xhr.responseText;
          if (retValue != null && retValue.length > 0) {
            console.log(retValue);
            rtpMultiCastConfigInfoParse(retValue);
          }
          else {
            console.log("结果为空");
          }
        }
      }
    };
    xhr.open("get", "/cgi-bin/htmlrequest.cgi?GET_RTPMULTICASTCONFIG_INFO");
    xhr.send(null);
  }
}

/*
 * 获取网络配置信息
 */
function getNetConfigInfo() {
  xhr = createXHR();
  if (xhr) {
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4) {
        if (xhr.status == 200) {
          var retValue = xhr.responseText;
          if (retValue != null && retValue.length > 0) {
            console.log(retValue);
            netConfigInfoParse(retValue);
          }
          else {
            console.log("结果为空");
          }
        }
      }
    };
    xhr.open("get", "/cgi-bin/htmlrequest.cgi?GET_NETCONFIG_INFO");
    xhr.send(null);
  }
}